var e = {
    d: (t, r) => {
      for (var n in r)
        e.o(r, n) &&
          !e.o(t, n) &&
          Object.defineProperty(t, n, { enumerable: !0, get: r[n] });
    },
    o: (e, t) => Object.prototype.hasOwnProperty.call(e, t),
  },
  t = {};
function r(e) {
  return new r.prototype.init(e);
}
e.d(t, { Z: () => n }),
  (r.prototype = {
    constructor: r,
    init: function (e) {
      (this.dbName = e),
        (this.version = +localStorage.getItem(this.dbName) || 1);
    },
    isSupportIndexDb() {
        return (
        "indexedDB" in window &&
        window.indexedDB.open(
          this.dbName,
          +localStorage.getItem(this.dbName) || 1
        )
      );
    },
    createDatabase() {
      return new Promise((e, t) => {
        const r = this.isSupportIndexDb();
        r.addEventListener("success", e), r.addEventListener("error", t);
      });
    },
    createTable(
      e,
      t = { keyPath: "id", autoIncrement: !1 },
      r = { options: { unique: !1 } }
    ) {
      return new Promise((n, s) => {
        this.version += 1;
        localStorage.setItem(this.dbName, this.version);
        try {
          this.isSupportIndexDb().addEventListener("upgradeneeded", (o) => {
            const a = o.target.result;
            if (a.objectStoreNames.contains(e))
              (this.version -= 1),
                s({ message: " table <" + e + "> name exists ", error: !0 });
            else {
              const s = a.createObjectStore(e, t);
              r?.fieldName &&
                s.createIndex(r.fieldName, r.fieldName, r.options),
                n({ message: "add table <" + e + "> success", success: !0 });
            }
          });
        } catch (t) {
          (this.version -= 1),
            s({ message: "add table <" + e + "> fail", error: !0 });
        }
      });
    },
    add(e, t, r) {
      return new Promise((n, s) => {
        this.commonPart(e, !0)
          .then((e) => {
            r || (t.id = (16777215 * Math.random()).toString(16).split(".")[1]);
            const o = e.add(t);
            o.addEventListener("success", (e) => n(t)),
              o.addEventListener("error", s);
          })
          .catch(s);
      });
    },
    commonPart(e, t) {
      return new Promise((r, n) => {
        const s = this.isSupportIndexDb();
        s.addEventListener("success", (n) => {
          const s = n.target.result,
            o = s.transaction(e, t ? "readwrite" : "readonly");
          let a = null;
          (a = arguments[2] ? s.createObjectStore(e) : o.objectStore(e)), r(a);
        }),
          s.addEventListener("error", n);
      });
    },
    selectKey(e, t) {
      return new Promise((r, n) => {
        this.commonPart(e)
          .then((e) => {
            const s = e.get(t);
            s.addEventListener("success", function (e) {
              r(this.result);
            }),
              s.addEventListener("error", n);
          })
          .catch(n);
      });
    },
    selectIndex(e, t, r) {
      return new Promise((n, s) => {
        this.commonPart(e)
          .then((e) => {
            var o = e.index(t).get(r);
            o.addEventListener("success", function (e) {
              n(o.result);
            }),
              o.addEventListener("error", s);
          })
          .catch(s);
      });
    },
    selectAll(e) {
      return new Promise((t, r) => {
        this.commonPart(e)
          .then((e) => {
            const n = e.openCursor();
            n.onerror = r;
            let s = [];
            (n.onsuccess = function (e) {
              var r = e.target.result;
              r ? (s.push(r.value), r.continue()) : t(s);
            }),
              n.addEventListener("error", r);
          })
          .catch(r);
      });
    },
    delete(e, t) {
      return new Promise(async (r, n) => {
        try {
          await this.commonPart(e);
          const  o = (await this.commonPart(e, !0)).delete(t);
          o.addEventListener("success", function (e) {
            r(s);
          }),
            o.addEventListener("error", n);
        } catch (e) {
          n(e);
        }
      });
    },
    put(e, t) {
      return new Promise(async (r, n) => {
        try {
          var s = (await this.commonPart(e, !0)).put(t);
          s.addEventListener("success", function (e) {
            r({ messgae: "update success", data: t });
          }),
            s.addEventListener("error", n);
        } catch (e) {
          n(e);
        }
      });
    },
  }),
  (r.prototype.init.prototype = r.prototype);
const n = r;
var s = t.Z;
export { s as default };
